﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlayBox
{
    public partial class ChangePassword : Form
    {

        public ChangePassword()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            btnCancel.Enabled = false;
            progressLogin.Visible = true;

            var t = new System.Threading.Tasks.Task(() =>
            {
                var success = User.ChangePassword(User.UserData.Username, txtPassword.Text);
                this.Invoke(new Action<bool>(ChangeFinished), success);
            });
            t.Start();
        }

        private void ChangeFinished(bool success)
        {
            if (!success)
            {
                btnOk.Enabled = true;
                btnCancel.Enabled = true;
                progressLogin.Visible = false;
            }
            else
            {
                MessageBox.Show("Passwort erfolgreich geändert!", "Erfolgreich", MessageBoxButtons.OK);
                this.Close();
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            CheckValid();
        }

        private void CheckValid ()
        {
            btnOk.Enabled = txtPassword.TextLength > 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
